package com.highstockings.dao;

import com.highstockings.model.Article;
import com.highstockings.model.ArticleDetailsVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("ArticleMapper")
public interface ArticleMapper {

    List<ArticleDetailsVo> selectArticleByCountry(@Param("country") String country);

    int deleteByPrimaryKey(Integer aid);

    int insert(Article record);

    int insertSelective(Article record);

    Article selectByPrimaryKey(Integer aid);

    int updateByPrimaryKeySelective(Article record);

    int updateByPrimaryKey(Article record);

    //基本信息列表
    @Select("select * from tb_article")
    List<Article> selectAllArticle();

    //随机5个页面图片信息
    @Select("SELECT aid,title,frontcover FROM tb_article WHERE tb_article.status='ON' ORDER BY rand() LIMIT 5;")
    List<Article> selectArticleImageInfo();


    List<ArticleDetailsVo> selectArticleByCondition(@Param(value = "columnName") String columnName);


    @Select("select aid,title,upcount,downcount,modelid,viewcount,commentcount,frontcover,tag from tb_article,tb_model" +
            " where tb_article.modelid=tb_model.mid and tb_article.status='ON' order by aid desc")
    List<ArticleDetailsVo> selectArticleHome();

    /*页面详细信息*/
    @Select("select aid,title,stars,createtime,filecounter,filenumber,filesize,downloadlink1,downloadlink2,viewcount,upcount,downcount,imglink1,imglink2,imglink3,\n" +
            "  frontcover,tag,status,librarytype as type ,username as authorName,alias as modelName from tb_article,tb_gallerytype,tb_model,tb_user\n" +
            "where tb_article.authorid=tb_user.uid and tb_article.modelid=tb_model.mid and tb_article.articletype=tb_gallerytype.tid and aid=#{args}")
    ArticleDetailsVo selectArticleDetailsById(int id);

    /*所有详细信息,后台用*/
    @Select("select *,librarytype as type ,username as authorName,alias as modelName from tb_article,tb_gallerytype,tb_model,tb_user\n" +
            "where tb_article.authorid=tb_user.uid and tb_article.modelid=tb_model.mid and tb_article.articletype=tb_gallerytype.tid order by createtime desc")
    List<ArticleDetailsVo> selectAllArticleDetails();

    /**
     * 增加观看数
     * @return
     */
    @Update("update tb_article set viewcount=viewcount+1 where aid=#{aid}" )
    int updateViewCount(int aid);

    @Update("update tb_article set status=#{status} where aid=#{aid}")
    int updateStatus(@Param("status") String status,@Param("aid") int aid);
    /**
     * 增加upcount
     * @return
     */
    @Update("update tb_article set upcount=upcount+1 where aid=#{aid}" )
    int updateUpCount(int aid);

    /**
     * 增加downcount
     * @return
     */
    @Update("update tb_article set downcount=downcount+1 where aid=#{aid}" )
    int updateDownCount(int aid);

    /**
     * Tag分类选取
     * @return
     */
    @Select("select tag from tb_article")
    List<String> selectTagName();

    /**
     * 标签查询
     * @return
     */
    @Select("select *,librarytype as type ,username as authorName,alias as modelName from tb_article,tb_gallerytype,tb_model,tb_user\n" +
            "where tb_article.authorid=tb_user.uid and tb_article.modelid=tb_model.mid and tb_article.articletype=tb_gallerytype.tid and tag like concat(concat('%',#{tag}),'%') and tb_article.status='ON' order by createtime desc")
    List<ArticleDetailsVo> selectArticleByTag(String tag);

    @Select("select *,librarytype as type,alias as modelName from tb_article,tb_gallerytype,tb_model where tb_article.modelid=tb_model.mid and tb_article.articletype=tb_gallerytype.tid and mid=#{mid} and tb_article.status='ON' order by createtime desc")
    List<ArticleDetailsVo> selectArticleByModelId(int mid);

    @Select("select aid,title,stars,imglink1,alias as modelName,librarytype as type from tb_article,tb_gallerytype,tb_model where tb_article.modelid=tb_model.mid and tb_article.articletype=tb_gallerytype.tid " +
            "and tb_article.status='ON' order by rank desc limit 10")
    List<ArticleDetailsVo> selectSideBarInfo1();
}
